﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Upreader.Usenet.Nntp.Encodings
{
    /// <summary>
    /// Represents a metaline found within a yenc encoded message
    /// </summary>
    public class YencMetaLine
    {
        private readonly string _line;

        public YencMetaLine(string line)
        {
            if (line == null)
                throw new ArgumentNullException("line");

            _line = line;
        }

        public string Name
        {
            get
            {
                int firstSpaceIndex = _line.IndexOf(' ');
                if (firstSpaceIndex < 0)
                    throw new ArgumentException("line", "A meta line should at least contain one space identifying the name of the line");

                string name = _line.Substring(0, firstSpaceIndex);

                return name;
            }
        }

        public IEnumerable<KeyValuePair<string, string>> Attributes
        {
            get
            {
                var query = from attribute in _line.Split(' ').Skip(1) // skip 1 to skip the initial name of the line
                            let keyValue = attribute.Split('=') // split on the key and value
                            where keyValue.Length > 1
                            select new KeyValuePair<string, string>(keyValue[0], keyValue[1]);

                return query;
            }
        }
    }
}
